package com.iflytek.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iflytek.common.Result;
import com.iflytek.dto.OrderGoodsDTO;
import com.iflytek.dto.PageDTO;
import com.iflytek.mapper.OrderGoodsMapper;
import com.iflytek.model.OrderGoods;
import com.iflytek.service.OrderGoodsService;
import com.iflytek.utils.CopyUtil;
import com.iflytek.vo.OrderGoodsVO;
import com.iflytek.vo.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 订单商品关系表 服务实现类
 * </p>
 *
 * @author h_t
 * @since 2023-09-23
 */
@Service
public class OrderGoodsServiceImpl extends ServiceImpl<OrderGoodsMapper, OrderGoods> implements OrderGoodsService {

    @Resource
    private OrderGoodsMapper orderGoodsMapper;

    /**
     * 查询
     *
     * @return
     */
    @Override
    public Result<List<OrderGoodsVO>> findAll() {
        QueryWrapper<OrderGoods> queryWrapper = new QueryWrapper();
        List<OrderGoods> orderGoodsList = orderGoodsMapper.selectList(queryWrapper);
        List<OrderGoodsVO> orderGoodsVOS = CopyUtil.copyList(orderGoodsList, OrderGoodsVO.class);
        return Result.success(orderGoodsVOS);
    }

    /**
     * 保存
     *
     * @param orderGoodsDTO
     * @return
     */
    @Override
    public Result<Boolean> saveOrderGoods(OrderGoodsDTO orderGoodsDTO) {
        if (orderGoodsDTO == null) {
            return Result.error("1", "数据不能为空");
        }
        OrderGoods orderGoods = CopyUtil.copy(orderGoodsDTO, OrderGoods.class);
        int count = orderGoodsMapper.insert(orderGoods);
        if (count > 0) {
            return Result.success(true);
        } else {
            return Result.error("2", "保存失败");
        }

    }

    /**
     * 更新
     *
     * @param orderGoodsDTO
     * @return
     */
    @Override
    public Result<Boolean> updateOrderGoods(OrderGoodsDTO orderGoodsDTO) {
        if (orderGoodsDTO == null) {
            return Result.error("1", "数据不能为空");
        }
        OrderGoods orderGoods = CopyUtil.copy(orderGoodsDTO, OrderGoods.class);
        int count = orderGoodsMapper.updateById(orderGoods);
        if (count > 0) {
            return Result.success(true);
        } else {
            return Result.error("2", "更新失败");
        }
    }

    /**
     * 分页
     *
     * @param pageDTO
     * @return
     */
    @Override
    public Result<PageVO<OrderGoodsVO>> findPage(PageDTO pageDTO) {
        QueryWrapper<OrderGoods> objectQueryWrapper = new QueryWrapper<>();
        if (pageDTO.getPageNum() == null) {
            pageDTO.setPageNum(1);
        }
        if (pageDTO.getSize() == null) {
            pageDTO.setSize(5);
        }
        PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getSize());
        List<OrderGoods> orderGoodsList = orderGoodsMapper.selectList(objectQueryWrapper);
        PageInfo<OrderGoods> pageInfo = new PageInfo<>(orderGoodsList);
        PageVO<OrderGoodsVO> pageVO = new PageVO<>();
        BeanUtils.copyProperties(pageDTO, pageVO);
        List<OrderGoodsVO> orderGoodsVOS = CopyUtil.copyList(orderGoodsList, OrderGoodsVO.class);
        pageVO.setTotal(pageInfo.getTotal());
        pageVO.setRecords(orderGoodsVOS);
        return Result.success(pageVO);
    }

    /**
     * ID查询
     *
     * @param id
     * @return
     */
    @Override
    public Result<OrderGoodsVO> findById(Integer id) {
        if (id == null) {
            return Result.error("1", "ID不能为空");
        }
        OrderGoods orderGoods = orderGoodsMapper.selectById(id);
        OrderGoodsVO orderGoodsVO = CopyUtil.copy(orderGoods, OrderGoodsVO.class);
        return Result.success(orderGoodsVO);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public Result<Boolean> delete(Integer id) {
        if (id == null) {
            return Result.error("1", "ID不能为空");
        }
        int count = orderGoodsMapper.deleteById(id);
        if (count > 0) {
            return Result.success(true);
        } else {
            return Result.error("2","删除失败");
        }
    }
}